// SPDX-FileCopyrightText: Copyright 2023 CitruS Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

package com.fanwj.citrus.applets.keyboard.ui

import android.app.Dialog
import android.content.DialogInterface
import android.os.Bundle
import android.text.InputFilter
import android.text.InputType
import androidx.fragment.app.DialogFragment
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.fanwj.citrus.R
import com.fanwj.citrus.applets.keyboard.SoftwareKeyboard
import com.fanwj.citrus.applets.keyboard.KeyboardConfig
import com.fanwj.citrus.applets.keyboard.KeyboardData
import com.fanwj.citrus.applets.keyboard.SwkbdResult
import com.fanwj.citrus.databinding.DialogEditTextBinding

class KeyboardDialogFragment(private val config: KeyboardConfig, private val callback: (KeyboardData) -> Unit) : DialogFragment() {
    private lateinit var binding: DialogEditTextBinding

    private val data = KeyboardData().apply { text = ""; result = SwkbdResult.Cancel }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        binding = DialogEditTextBinding.inflate(layoutInflater)

        // Set up the input
        binding.editText.hint = config.initial_text
        binding.editText.isSingleLine = !config.enable_return_button
        binding.editText.filters =
            arrayOf<InputFilter>(InputFilter.LengthFilter(config.max_text_length))

        // Handle input type
        var inputType: Int
        when (config.type) {
            SoftwareKeyboard.SwkbdType.Normal.ordinal,
            SoftwareKeyboard.SwkbdType.Qwerty.ordinal,
            SoftwareKeyboard.SwkbdType.Unknown3.ordinal,
            SoftwareKeyboard.SwkbdType.Latin.ordinal,
            SoftwareKeyboard.SwkbdType.SimplifiedChinese.ordinal,
            SoftwareKeyboard.SwkbdType.TraditionalChinese.ordinal,
            SoftwareKeyboard.SwkbdType.Korean.ordinal -> {
                inputType = InputType.TYPE_CLASS_TEXT
                if (config.password_mode == SoftwareKeyboard.SwkbdPasswordMode.Enabled.ordinal) {
                    inputType = inputType or InputType.TYPE_TEXT_VARIATION_PASSWORD
                }
            }
            SoftwareKeyboard.SwkbdType.NumberPad.ordinal -> {
                inputType = InputType.TYPE_CLASS_NUMBER
                if (config.password_mode == SoftwareKeyboard.SwkbdPasswordMode.Enabled.ordinal) {
                    inputType = inputType or InputType.TYPE_NUMBER_VARIATION_PASSWORD
                }
            }
            else -> {
                inputType = InputType.TYPE_CLASS_TEXT
                if (config.password_mode == SoftwareKeyboard.SwkbdPasswordMode.Enabled.ordinal) {
                    inputType = inputType or InputType.TYPE_TEXT_VARIATION_PASSWORD
                }
            }
        }
        binding.editText.inputType = inputType

        val headerText =
            config.header_text!!.ifEmpty { resources.getString(R.string.software_keyboard) }
        val okText =
            config.ok_text!!.ifEmpty { resources.getString(R.string.submit) }

        return MaterialAlertDialogBuilder(requireContext())
            .setTitle(headerText)
            .setView(binding.root)
            .setPositiveButton(okText) { _, _ ->
                data.result = SwkbdResult.Ok
                data.text = binding.editText.text.toString()
            }
            .setNegativeButton(resources.getString(android.R.string.cancel)) { _, _ ->
            }
            .create()
    }

    override fun onDismiss(dialog: DialogInterface) {
        super.onDismiss(dialog)
        callback(data)
    }
}
